package com.wql.mail;

import java.io.File;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 * 邮件操作工具类
 *
 * @author wuqinglong
 * @time 下午10:53:34
 * @email hellowql@126.com
 */
public class MailUtils {
	private static JavaMailSender javaMailSender;
	private static SimpleMailMessage mailMessage;
	private static VelocityEngine velocityEngine;
	static {
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("com/wql/mail/mail.xml");
		javaMailSender = ctx.getBean("javaMailSender", JavaMailSender.class);
		mailMessage = ctx.getBean("mailMessage", SimpleMailMessage.class);
		velocityEngine = ctx.getBean("velocityEngine", VelocityEngine.class);
	}

	/**
	 * 简单发送文本信息
	 */
	public static void sendText() {
		SimpleMailMessage mail = new SimpleMailMessage();
		try {
			mail.setTo("hellowql@126.com");// 接受者
			mail.setFrom(mailMessage.getFrom());// 发送者,这里还可以另起Email别名，不用和xml里的username一致
			mail.setSubject("wql-Mail");// 主题
			mail.setText("wql-Mail-哈哈哈！");// 邮件内容
			send(mail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送html信息
	 * 
	 * @throws MessagingException
	 */
	public static void sendHTML() throws MessagingException {
		// 获取JavaMailSender bean
		MimeMessage mailMessage = javaMailSender.createMimeMessage();
		// 设置utf-8或GBK编码，否则邮件会有乱码
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
		try {
			messageHelper.setTo("hellowql@126.com");// 接受者
			messageHelper.setFrom("hellowql@126.com");// 发送者
			messageHelper.setSubject("wql-Mail-html");// 主题
			// 邮件内容，注意加参数true，表示启用html格式
			messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>", true);
			send(mailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送带附件的信息
	 * 
	 * @throws MessagingException
	 */
	public static void sendAttachment() throws MessagingException {
		// 获取JavaMailSender bean
		MimeMessage mailMessage = javaMailSender.createMimeMessage();
		// 设置utf-8或GBK编码，否则邮件会有乱码
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
		try {
			messageHelper.setTo("hellowql@126.com");// 接受者
			messageHelper.setFrom("hellowql@126.com");// 发送者
			messageHelper.setSubject("wql-Mail-html");// 主题
			// 邮件内容，注意加参数true
			messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>", true);
			// 附件内容
			messageHelper.addInline("a", new File("F:/822609.jpg"));
			messageHelper.addInline("b", new File("F:/232343.jpg"));
			File file = new File("F:/A.rar");
			// 这里的方法调用和插入图片是不同的，使用MimeUtility.encodeWord()来解决附件名称的中文问题
			messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
			send(mailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 使用velocity模板发送文本信息
	 * 
	 * @param model
	 */
	public static void sendWithTemplate(Map<String, Object> model) {
		mailMessage.setTo("hellowql@126.com"); // 接收人
		mailMessage.setSubject("wql-Mail-vm");
		String result = null;
		try {
			result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mail.vm", "UTF-8", model);
		} catch (Exception e) {
		}
		mailMessage.setText(result);
		send(mailMessage);
	}

	/**
	 * 异步发送
	 * 
	 * @param <T>
	 * 
	 * @param mess
	 */
	private static <T> void send(final T mess) {
		new Thread() {
			public void run() {
				if (mess instanceof MailMessage)
					javaMailSender.send((SimpleMailMessage) mess);
				else if (mess instanceof MimeMessage)
					javaMailSender.send((MimeMessage) mess);
			}
		}.start();
	}

	public static void main(String[] args) throws MessagingException {
		// sendText();
		sendHTML();
		// sendAttachment();
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("msg", "hello");
		// sendWithTemplate(map);
	}
}
